package com.augurit.common.base.action;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.opensymphony.xwork2.ActionSupport;
import com.thoughtworks.xstream.XStream;

/**
 * Action 基类，提供基础的Action方法 和 JSON及XML支持
 */
public class BaseAction extends ActionSupport{
	
	/**
	 * 在log4j.properties中,本logger已被指定使用asyncAppender.
	 */
	public static final String DB_LOGGER_NAME = "AosExceptionDBLog";
	
	
	/**
	 * 存放登录用户信息
	 * @param loginUser
	 */
	public void setUserSession(LoginUserForm loginUser){
		this.getSession().setAttribute(WebContextUtils.SES_LOGIN_USER, loginUser);
	}
	
	/**
	 * 获取登录用户信息
	 * @return
	 */
	public LoginUserForm getLoginUser(){
		return (LoginUserForm)this.getSession().getAttribute(WebContextUtils.SES_LOGIN_USER);
	}
	
	/**
	 * 获取登录用户ID
	 * @return
	 */
	public Long getLoginUserId(){
		LoginUserForm loginUserForm = getLoginUser();
		if(loginUserForm != null)
			return loginUserForm.getUser().getUserId();
		else
			return null;
	}
	
	/**
	 * 获取登录用户名称
	 * @return
	 */
	public String getLoginUserName(){
		LoginUserForm loginUserForm = getLoginUser();
		if(loginUserForm != null)
			return loginUserForm.getUser().getLoginName();
		else
			return null;
	}
	
	
	// 取得Request/Response/Session的简化函数 //

	/**
	 * 取得HttpSession的简化方法.
	 */
	public static ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}
	
	/**
	 * 取得HttpSession的简化方法.
	 */
	public static HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * 取得HttpSession的简化函数.
	 */
	public static HttpSession getSession(boolean isNew) {
		return ServletActionContext.getRequest().getSession(isNew);
	}

	/**
	 * 取得HttpSession中Attribute的简化函数.
	 */
	public static Object getSessionAttribute(String name) {
		HttpSession session = getSession(false);
		return (session != null ? session.getAttribute(name) : null);
	}
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	public static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 取得协议+服务器名+端口号
	 */
	public static String getRootPath(){
		return ServletActionContext.getRequest().getScheme()
			   + "://"
			   + ServletActionContext.getRequest().getServerName()
			   + ":" 
			   + ServletActionContext.getRequest().getServerPort();
	}

	/**
	 * 取得Request Parameter的简化方法.
	 */
	public static String getParameter(String name) {
		return getRequest().getParameter(name);
	}
	
	public static String getAbsolutePath(){
		String absolutePath = getApplication().getRealPath("/").replaceAll("\\\\", "/");
		return absolutePath.substring(0, absolutePath.length()-1);
	}

	// 绕过jsp/freemaker直接输出文本的函数 //

	/**
	 * 直接输出内容的简便函数.

	 * eg.
	 * render("text/plain", "hello", "encoding:GBK");
	 * render("text/plain", "hello", "no-cache:false");
	 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
	 * 
	 * @param headers 可变的header数组，目前接受的值为"encoding:"或"no-cache:",默认值分别为UTF-8和true.
	 */
	public static void render(final String contentType, final String content, final String... headers) {
		Struts2Utils.render(contentType, content, headers);
	}

	/**
	 * 直接输出文本.
	 * @see #render(String, String, String...)
	 */
	public static void renderText(final String text, final String... headers) {
		Struts2Utils.renderText(text, headers);
	}

	/**
	 * 直接输出HTML.
	 * @see #render(String, String, String...)
	 */
	public static void renderHtml(final String html, final String... headers) {
		Struts2Utils.renderHtml(html, headers);
	}

	/**
	 * 直接输出XML.
	 * @see #render(String, String, String...)
	 */
	public static void renderXml(final String xml, final String... headers) {
		Struts2Utils.renderXml(xml, headers);
	}

	/**
	 * 直接输出JSON.
	 * 
	 * @param string json字符串.
	 * @see #render(String, String, String...)
	 */
	public static void renderJson(final String string, final String... headers) {
		Struts2Utils.renderJson(string, headers);
	}
	
	/**
	 * 直接输出JSON,使用Jackson转换Java对象.
	 * 
	 * @param data 可以是List<POJO>, POJO[], POJO, 也可以Map名值对.
	 * @see #render(String, String, String...)
	 */
	public static void renderJson(final Object data, final String... headers) {
		Struts2Utils.renderJson(data, headers);
	}
	
	public static String getJsonString(final Object data){
		return Struts2Utils.getJsonString(data);
	}
	
	/**
	 * 将对象转换为XML输出
	 * @param object 对象
	 * @param aliasName 类别名到XML元素名的映射
	 * @param objectClass 对象的Class
	 * @return
	 */
	public String renderXml(final Object object, final String aliasName, final Class objectClass){
		XStream xstream = new XStream();
		xstream.setMode(XStream.NO_REFERENCES);
		xstream.alias(aliasName, objectClass);
		return xstream.toXML(object);
	}

	/**
	 * 将JSON字符串转换为对象列表
	 * @param jsonString
	 * @param beanClass
	 * @return
	 * @throws Exception 
	 */
	public static <T> T buildFromJson(final String jsonString, final Class<T> beanClass) throws Exception{
		return Struts2Utils.buildFromJson(jsonString, beanClass);
	}
	
	/**
	 * 根据JSON字符串转换为多个对象
	 * @param <T>
	 * @param jsonString
	 * @param beanClass
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> buildListFromJson(final String jsonString, final Class<T> beanClass) throws Exception{
		return Struts2Utils.buildListFromJson(jsonString, beanClass);
	}
}
